-
Notifications
You must be signed in to change notification settings - Fork 634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(collection): add findSingle #1166
Conversation
update doc
e345002
to
820e27b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks very good to me! Includes all the important easily overlooked details, well written tests, docs...
Thank you very much :-) I hope @kt3k agrees^^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@NotFounds Thank you for your contribution.
I generally agree with this addition. Left a few comments.
collections/README.md
Outdated
### single | ||
|
||
Returns the only element in the given collection matching the given predicate. | ||
Returns undefined if there is none. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should mention more explicitly about returning undefined when there are multiple matches
and it might be good to have another example code block which has multiple matches
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little concerned about the name of the function. It sounds like an adjective to me. (We discussed this topic in #1098 ) Does anyone have any other suggestion for the name of this? |
Kotlin calls it single, hence the initial name . I personally see why, it reads well when used in context. Maybe |
collections/README.md
Outdated
### single | ||
|
||
Returns the only element in the given collection matching the given predicate. | ||
Returns undefined if there is none or multiple matches for the predicate. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alternative:
Returns the element if and only if a single matching element exists to the given condition; otherwise, it returns undefined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"to the given condition" does not make sense to me (not a native speaker though). Maybe slightly modify to
Returns an element if and only if that element is the only one matching the given condition.
Returns `undefined` otherwise.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for comment! Fixed it! 1fa2dbb
@kt3k I think this is approaching mergability - what should we do about the name? |
I started feeling the name is ok as is because Does anyone have any opinion about this? cc @timreichen |
@kt3k |
If you read const file = findDistinct(Deno.args) do you feel like you would know what's going on here? In my (non-native speaker) mind the word distinct needs other values to make sense and does not really make sense for a one-element array. |
@timreichen Thank you for the input, but findDistinct sounds a little strange to me.. How about |
@kt3k This will make deno std attractive for people that do partnership search:) I don't know, but better than just const file = findDistinct(Deno.args) tbh not so much since the default predicate is set to In that sense I think having this default predicate changes the meaning of the function to const value = findDistinct(["f", "o", "o"], (char) => char === "o") // undefined const value = findDistinct(["f", "o", "o"], (char) => char === "f") // "f" btw: if (match !== undefined) return undefined;` Hypothetically this will not short circuit for undefined values: const array = new Array(1000).fill(undefined)
const value = findDistinct(array, (i) => i === undefined); // predicate called 1000x Maybe a flag should be used instead? let match: T | undefined = undefined;
let found = false
for (const element of array) {
if (predicate(element)) {
if (found) return undefined;
found = true
match = element;
}
}
return match; |
Adoption boost spotted^^
Yeah that is what I meant - find does not fit that well but it is an essential part of the usage/signature (stolen from kotlin again).
Oh yes, we should change that @NotFounds . Good catch! |
@timreichen Fixed it! 8864057 |
Should be mergeable now @kt3k |
Can we settle on the name 'single'? @NotFounds Do you have any preference on naming? |
(I'm not a native speaker) But... if a verb is preferred, the suggested "findSingle" is the best fit for me. |
|
Sounds like @kt3k I think we need your/some core call here. |
Note NotFounds: single or findSingle (new votes are welcome!) |
@kt3k |
I dont think we should remove functionality for a name, but find a name for functionality |
I think To make sure a single value exists in an array |
Typings make that
I mean you can always write more code instead of using something that encapsulates it, sure. Saying "should" here is a stretch to me though - why "should" someone not use that function if they want to? It might not be your style, but it might be theirs and it is hard to say someone objectively "should" not do it. Either way, I think I would be fine with |
Let's settle on the name Let's continue the discussion of the default selector in another issue. I'm merging this for now as this one is enough discussed and looks having enough quality now. Thank you for your efforts! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@NotFounds LGTM. Thank you for your contribution!
Implement
single
for collection module.(ref: #1065)
The type definition is based on this.
ref.
#1065 (comment)
#978